# Makefile to build the builder
# Written by Aaron J. Miller 20111102
#
# Invoke with CONFIG=RELEASE for the release version; defaults to debug

CC             := cc
LD             := $(CC)

CFLAGS_DEBUG   := -g -DDEBUG -D_DEBUG
LFLAGS_DEBUG   :=

CFLAGS_RELEASE := -O3 -DNDEBUG
LFLAGS_RELEASE := -s

ifeq ($(CONFIG),)
 CONFIG        := DEBUG
endif

CFLAGS         := $(CFLAGS_$(CONFIG)) -W -Wall -pedantic -std=c99 -c -MD -MP
LFLAGS         := $(LFLAGS_$(CONFIG))

SRCDIR         := source

SOURCES        := $(wildcard $(SRCDIR)/*.c)
OBJECTS        := $(SOURCES:.c=.o)
DEPENDS        := $(OBJECTS:.o=.d)

ifdef OS
 ifeq ($(OS),Windows_NT)
  SUFFIX       := .exe
 else
  ifdef SystemRoot
   SUFFIX      := .exe
  else
   SUFFIX      :=
  endif
 endif
else
 SUFFIX        :=
endif
TARGET         := bin/mk$(SUFFIX)

.PHONY: all clean

all: $(TARGET)

clean:
	-@rm -rf $(wildcard $(TARGET) $(OBJECTS) $(DEPENDS))

$(TARGET): $(OBJECTS)
	$(info [LD] $@)
	@$(LD) $(LFLAGS) -o $@ $+

%.o: %.c
	$(info [CC] $@)
	@$(CC) $(CFLAGS) -o $@ $<

ifneq ($(MAKECMDGOALS),clean)
 -include $(DEPENDS)
endif
